package com.grapecity.control.gridview;

import java.util.ArrayList;
import java.util.Arrays;

import com.grapecity.control.gridview.CircleGridView.OnImageClickListener;
import com.grapecity.control.util.DownloadImageTask;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter implements OnImageClickListener {
	protected Context context;
	private ArrayList<String> items;

	public OnGridViewClickListener gridViewClickListener;

	public interface OnGridViewClickListener {
		public void onGridViewCilcked(int position, ArrayList<String> items);
	}

	public ImageAdapter(Context context) {
		this.context = context;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView view;

		if (convertView != null) {
			view = (ImageView) convertView;
		} else {
			view = generialDefaultImageView();
		}

		view.setScaleType(ImageView.ScaleType.CENTER_CROP);
		new DownloadImageTask(view).execute((String) getItem(position));
		return view;
	}

	public ImageView generialDefaultImageView() {
		ImageView imageView = new ImageView(context);
		imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
		ViewGroup.LayoutParams params = generialDefaultLayoutParams();
		imageView.setLayoutParams(params);

		return imageView;
	}

	protected ViewGroup.LayoutParams generialDefaultLayoutParams() {
		return new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
	}

	public void setItems(String[] strings) {
		this.items = new ArrayList<String>(Arrays.asList(strings));
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public void onImageCilcked(int position, View view) {
		// TODO Auto-generated method stub
		if (this.gridViewClickListener != null) {
			this.gridViewClickListener.onGridViewCilcked(position, items);
		}
	}

	public void setGridViewClickListener(OnGridViewClickListener listener) {
		this.gridViewClickListener = listener;
	}

}